<?php

namespace app\teacher\controller\grammar;

use app\common\controller\Frontend;
use app\common\service\grammar\DetectionService;
use app\common\model\banxi\grammar\GrammarDetection;
use app\common\model\banxi\grammar\GrammarDetectionAnswer;
use app\common\model\banxi\grammar\GrammarQuestion;
class GrammarLearn extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 开始检测
     * */
    public function startDetection()
    {
        $userId = $this->request->param('userId', 0);
        $categoryId = $this->request->param('categoryId', 0);
        $isReset = $this->request->param('isReset');
        $isReset = $isReset == 'true' ? true : false;
        if (!$categoryId) {
            $this->fail('语法分类不能为空');
        }
        $userId = $userId ? $userId : $this->auth->id;
        $DetectionService = new DetectionService();
        $result = $DetectionService->startDetection($userId, $categoryId, $isReset);
        if (!$result){
            $this->fail($DetectionService->getError());
        }
        $this->ok('操作成功', $result);
    }

    /*
     * 查询-当前未做的第一题
     * */
    public function getCurrentQuestion()
    {
        $detectionId = $this->request->get('detectionId', 0);

        $DetectionService = new DetectionService();
        $result = $DetectionService->getCurrentQuestion($detectionId);
        if (!$result){
            $this->fail($DetectionService->getError());
        }
        $this->ok('操作成功', $result);
    }

    /*
     * 提交答案
     * */
    public function submitQuestion()
    {
        $userId = $this->request->param('userId');
        $detectionId = $this->request->param('detectionId');
        $questionId = $this->request->param('questionId');
        $userAnswerList = $this->request->param('userAnswerList');
        $useTime = $this->request->param('useTime');

        if (!$detectionId){
            $this->fail('检测id不能为空');
        }
        $answer = [
            'detectionId' => $detectionId,
            'questionId' => $questionId,
            'userAnswerList' => $userAnswerList,
            'useTime' => $useTime
        ];
        $userId = $userId ? $userId : $this->auth->id;
        $DetectionService = new DetectionService();
        $result = $DetectionService->submitQuestion($userId,$answer);
        if (!$result){
            $this->fail($DetectionService->getError());
        }
        $this->ok('操作成功', $result);
    }

    /*
     * 查询-检测结果
     * */
    public function getDetectionResult()
    {
        $detectionId = $this->request->get('detectionId');
        if (!$detectionId){
            $this->fail('检测id不能为空');
        }
        $GrammarDetectionModdel = new GrammarDetection();
        $result = $GrammarDetectionModdel->with(['questionList'])->where('id', $detectionId)->find();

        $this->ok('操作成功', $result);
    }

    /*
     * 查询-答题记录信息
     * */
    public function getDetectionAnswerInfo()
    {
        $answerId = $this->request->get('answerId');
        if (!$answerId){
            $this->fail('答题记录ID不能为空');
        }
        $GrammarDetectionAnswerModdel = new GrammarDetectionAnswer();
        $result = $GrammarDetectionAnswerModdel->with(['questionInfo'])->where('id', $answerId)->find();
        $result['userAnswerList'] = json_decode($result['userAnswer'], true);
        $result['questionInfo']['optionList'] = json_decode($result['questionInfo']['option'], true);
        $result['questionInfo']['answerList'] = json_decode($result['questionInfo']['answer'], true);

        $this->ok('操作成功', $result);
    }
}